package com.situ.student.service.impl;

import java.lang.reflect.AnnotatedWildcardType;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

import com.situ.student.dao.IBanjiDao;
import com.situ.student.dao.impl.BanjiDaoJdbcImpl;
import com.situ.student.entity.Banji;
import com.situ.student.entity.PageBean;
import com.situ.student.service.IBanjiService;


public class BanjiServiceImpl implements IBanjiService {
	private IBanjiDao banjiDao =  new BanjiDaoJdbcImpl();
	
	//展示
	@Override
	public List<Banji> list() {
		return banjiDao.list();
	}
	
	//根据id删除
	@Override
	public boolean deleteById(Integer id) {
		int count = banjiDao.deleteById(id);
		return count == 1 ? true : false; //返回true 或 falae
	}
	
	//插入数据
	@Override
	public boolean insert(Banji banji) {
		int count = banjiDao.insert(banji);
		return count == 1 ? true : false;
	}
	
	//修改前展示数据
	@Override
	public Banji findById(Integer id) {
		return banjiDao.findById(id);
	}
	
	//修改
	@Override
	public boolean update(Banji banji) {
		int count = banjiDao.update(banji);
		return count == 1 ? true : false;
	}
	
	//批量删除
	@Override
	public boolean deleteAll(String[] selectIds) {
		int count = banjiDao.deleteAll(selectIds);
		return count == selectIds.length ? true : false;
	}

	//分页
	@Override
	public PageBean<Map<String, Object>> getPagBean(int pageNo, int pageSize) {
		//new对象
		PageBean<Map<String, Object>> pageBean = new PageBean<Map<String, Object>>();
		//当前页数
		pageBean.setPageNo(pageNo);
		//页的数据
		pageBean.setPageSize(pageSize);
		//总的记录数
		int totalCount = banjiDao.getTotalCount();
		pageBean.setTotalCount(totalCount);
		//一共多少页
		int totalPage = (int)Math.ceil((double)totalCount/pageSize);
		pageBean.setTotalPage(totalPage);
		//当前页的数据
		int offset = (pageNo - 1) * pageSize;
		List<Map<String, Object>> list = banjiDao.pageList(offset,pageSize);
		pageBean.setList(list);
		return pageBean;
	}

	@Override
	public boolean checkName(String name) {
		int count =  banjiDao.selectCountByName(name);
		return count > 0 ? true : false;
		}
}


